//
//  Double+Extension.swift
//  PMS
//
//  Created by 何海涛 on 15/10/20.
//  Copyright © 2015年 途家. All rights reserved.
//

import Foundation

extension Double {
    
    public static let mathRound0 = "%.0f"
    public static let mathRound1 = "%.1f"
    public static let mathRound2 = "%.2f"
    
    public func toString(_ format : String) -> String{
        return String(format: format, self)
    }
    
    public func toMoney() -> String {
        let formatter = NumberFormatter()
        formatter.numberStyle = .currency
        formatter.currencySymbol = CommonDefine.kCurrencySymbol
        formatter.allowsFloats = false
        return formatter.string(from: NSNumber(value: self))!
    }
}

extension Double {
    /** 如果是整数则只显示整数部分，否则显示小数。separator为千位分隔符，例如可以传逗号或空格等。 */
    public func toPrice(_ separator: String = "") -> String {
        let formatter = NumberFormatter()
        formatter.numberStyle = NumberFormatter.Style.decimal
        formatter.groupingSeparator = separator
        return formatter.string(from: NSNumber(value: self))!
    }
}

extension Double{
    
    public func toRoundStr(_ decimalPlaces:Int) -> String {
        let val = Darwin.round(self*pow(10, Double(decimalPlaces)))/pow(10, Double(decimalPlaces))
        return val.toString("%."+"\(decimalPlaces)"+"f")
    }
    
}
